package xin.lhuo.we.server.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.lhuo.we.server.entity.Today;
import xin.lhuo.we.server.entity.User;
import xin.lhuo.we.server.service.ITodayService;
import xin.lhuo.we.server.utils.ResultVoUtil;
import xin.lhuo.we.server.vo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static xin.lhuo.we.server.constant.HttpConstant.SESSION_USER;

/**
 * @author Lhuo
 * @date 2019/6/18 0:49
 */
@RestController
@RequestMapping(value = "/today")
public class TodayController {
    @Autowired
    private ITodayService iLoveService;

    @GetMapping(value = "/")
    public ResultVo getToday(HttpServletRequest request) {
        return ResultVoUtil.success(iLoveService.getTodayList(getUser(request).getId()));
    }

    @PostMapping(value = "/")
    public ResultVo createToday(Today today, HttpServletRequest request) {
        User user = getUser(request);
        today.setUserId(user.getId());
        iLoveService.createToday(today);
        return ResultVoUtil.success();
    }

    private User getUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return (User) session.getAttribute(SESSION_USER);
    }
}
